#include "AudioManager.h"
#include "Sound.h"

#include <stdlib.h>
#include <algorithm>

irrklang::ISoundEngine* AudioManager::soundEngine = NULL;
AudioManager * AudioManager::_instance = NULL;
std::string AudioManager::filePath = "assets/";

AudioManager::AudioManager()
{
	// start up the engine
	soundEngine = irrklang::createIrrKlangDevice();
	if(!soundEngine)
		throw "Sound Engine could not be started";
	_masterVolume = (soundEngine->getSoundVolume() * 100.0);
}

AudioManager::~AudioManager()
{
	// after finished,
	// close the engine again, similar as calling 'delete'
	soundEngine->drop();
}

AudioManager * AudioManager::Instance()
{
	return _instance;
}

void AudioManager::create()
{
	if (_instance == NULL)
	{
		_instance = new AudioManager();		
	}
}

void AudioManager::destroy()
{
	delete _instance;
	_instance = NULL;
}

irrklang::ISoundEngine* AudioManager::getEngine() const
{
	return soundEngine;
}

Sound* AudioManager::load(std::string name)
{
	std::vector<Sound*>::iterator it;
	for(it = soundSourcesContainer.begin(); it !=soundSourcesContainer.end(); ++it)
	{
		if((*it)->getName().compare(name) == 0)
		{
			return (*it);
		}
	}
	
	Sound* tempSound = NULL;
	std::string tempPath = filePath;
	tempSound = new Sound(name, tempPath.append(name));
	if(!tempSound)
	{
		throw "Sound could not be loaded";
	}else{
		soundSourcesContainer.push_back(tempSound);
	}
	return tempSound;
}
void AudioManager::unload(std::string name)
{
	std::vector<Sound*>::iterator it;
	for(it = soundSourcesContainer.begin(); it !=soundSourcesContainer.end(); ++it)
	{
		if((*it)->getName().compare(name) == 0)
		{
			delete (*it);
			soundSourcesContainer.erase(it);
			break;
		}
	}
}

Sound* AudioManager::getSound(std::string name) const
{
	std::vector<Sound*>::const_iterator it;
	for(it = soundSourcesContainer.begin(); it !=soundSourcesContainer.end(); ++it)
	{
		if((*it)->getName().compare(name) == 0)
		{
			return (*it);
		}
	}
	return NULL;
}

void AudioManager::pauseAll()
{
	soundEngine->setAllSoundsPaused();
}

void AudioManager::stopAll()
{
	soundEngine->stopAllSounds();
}

void AudioManager::setBackgroundSound(std::string nameOfBackgroundSound)
{
	backgroundSound = getSound(nameOfBackgroundSound);
	if(backgroundSound == NULL)
	{
		throw "No background sound like this";
	}
}
void AudioManager::playBackgroundSound()
{
	backgroundSound->setIsLooped(true);
	backgroundSound->play();
}

void AudioManager::setMasterVolume(int newVolume)
{
	//check the value for between 0 and 100
	if(newVolume < 0 || newVolume > 100)
        _masterVolume = 50;
    else
        _masterVolume = newVolume;
    soundEngine->setSoundVolume(_masterVolume/100.0);
}
void AudioManager::increaseMasterVolume()
{
	_masterVolume += 10;
    if(_masterVolume > 100)
        _masterVolume = 100;
    soundEngine->setSoundVolume(_masterVolume/100.0);
}
void AudioManager::increaseMasterVolume(int increment)
{
	_masterVolume += increment;
    if(_masterVolume > 100)
        _masterVolume = 100;
    soundEngine->setSoundVolume(_masterVolume/100.0);
}
void AudioManager::decreaseMasterVolume()
{
	_masterVolume -= 10;
    if(_masterVolume < 0)
        _masterVolume = 0;
    soundEngine->setSoundVolume(_masterVolume/100.0);
}
void AudioManager::decreaseMasterVolume(int increment)
{
	_masterVolume -= increment;
    if(_masterVolume < 0)
        _masterVolume = 0;
    soundEngine->setSoundVolume(_masterVolume/100.0);
}
int AudioManager::getCurrentMasterVolume() const
{
	return _masterVolume;
}

void AudioManager::onKeyPressed(int key)
{
	if(key == 'N' || key == 'n')
	{
		backgroundSound->decreaseVolume();
	}
	if(key == 'M' || key == 'm')
	{
		backgroundSound->increaseVolume();
	}
}
	
void AudioManager::onKeyReleased(int key){}